#include <iostream>
#include <vector>
#include <set>

using namespace std;

set<string> generateDistinctSubStrings(string inputString)
{
	set<string> finalResult;
	const int length = inputString.size();
	for ( int i = 0 ; i < length ; ++i)
	{
		for ( int len = 1 ; len <=length; ++ len)
		{
			string localResult = inputString.substr(i,len) ;
			finalResult.insert(localResult);
		}
	}
	return finalResult;
}
vector<string> distinctSubstring(string inputString)
{
	vector<string> finalResult;
	vector<string> previousStepResult;
	for (int i = 0 ; i < inputString.size() ; ++i)
	{
		vector<string> currentStepResult;
		string curChar = string() + inputString[i];
		currentStepResult.push_back(curChar);
		for(int j = 0; j < previousStepResult.size(); ++j)
		{
			string result = previousStepResult[j] + curChar;
			currentStepResult.push_back(result);
		}
		finalResult.insert(finalResult.end(),currentStepResult.begin(),currentStepResult.end());
		previousStepResult = currentStepResult;
	}
	return finalResult;
}
int main()
{
	//set<string> distinctSubStrs = generateDistinctSubStrings("KKKK");
	//set<string>::iterator itr= distinctSubStrs.begin();
	//for(;itr!=distinctSubStrs.end();itr++)
	//{
	//cout<<*itr<<endl;
	//}

	vector<string> distinctSubStrVector = distinctSubstring("KLMN");
	vector<string>::iterator itr2= distinctSubStrVector.begin();
	for(;itr2!=distinctSubStrVector.end();itr2++)
	{
		string result = *itr2;
		cout<<result<<endl;
	}

	return 0;
}